package com.xixibaby.fadada.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.net.URLEncoder;
import com.alibaba.fastjson.JSONObject;
import com.xixibaby.fadada.ContractOnline;
import com.xixibaby.fadada.dto.AccountVervify;
import com.xixibaby.fadada.dto.CompanyVervify;
import com.xixibaby.fadada.dto.Contract;
import com.xixibaby.fadada.dto.VervifyUrl;
import org.apache.commons.codec.Charsets;


/**
 * 基于法大大的在线合同签订实现
 * @author tooomj
 */
public class FadadaContractOnlineImpl extends FadadaApiClient implements ContractOnline {

    private final static String EMPTY_STR = "";


    @Override
    public VervifyUrl getCompanyVervifyUrl(String customerId, String companyName) {
        VervifyUrl vervifyUrl = this.invork(FadadaUrl.COMPANY_VERIFY_URL, params -> {
            params.put("customer_id", customerId);
            params.put("verified_way", getCompanyVerifyWay());
            params.put("page_modify", "1");
            params.put("notify_url", getVerifyNodifyUrl());
            params.put("return_url", getVefiryReturnUrl());
            params.put("company_principal_type", "1");

            params.put("result_type", EMPTY_STR);
            params.put("cert_flag", EMPTY_STR);

            JSONObject object = new JSONObject();
            object.put("company_name",companyName);
            params.put("company_info",object.toJSONString());
            params.put("legal_info","{}");

        }, VervifyUrl.class);

        String url = vervifyUrl.getUrl();
        String bizContent = URLDecoder.decode(url,Charsets.UTF_8);
        url = new String(Base64.decode(bizContent.getBytes()));
        vervifyUrl.setUrl(url);
        return vervifyUrl;
    }

    //0:取消授权, 1:授权
    @Override
    public void authorization(String companyId, String personId) {
        this.invork(FadadaUrl.AUTH_URL,param -> {
            param.put("a", companyId);
            param.put("b", personId);
            param.put("c", 1);
        },Void.class,p -> {
            p.put("company_id", companyId);
            p.put("person_id", personId);
            p.put("operate_type", 1);
        },"");
    }

    @Override
    public void unAuthorization(String companyId, String personId) {
        this.invork(FadadaUrl.AUTH_URL,param -> {
            param.put("a", companyId);
            param.put("b", personId);
            param.put("c", 0);
        },Void.class,p -> {
            p.put("company_id", companyId);
            p.put("person_id", personId);
            p.put("operate_type", 0);
        },"");
    }

    @Override
    public void autoSignContract(String transId, String customerId, String contractCode) {
        this.invork(FadadaUrl.AUTO_SIGN,param -> {
            param.put("customer_id", customerId);
        },String.class, param -> {
            param.put("transaction_id", transId);
            param.put("contract_id", contractCode);
            param.put("outh_customer_id", EMPTY_STR);
            param.put("client_role", "1");
            param.put("doc_title", EMPTY_STR);
            param.put("position_type", "0");
            param.put("sign_keyword", getAutoKeyWord());
            param.put("notify_url", getSignNodifyUrl());
            param.put("keyword_strategy", "0");
        },null, transId);
    }

    @Override
    public String registCompanyAccount(Long userId) {
        return this.invork(FadadaUrl.REGIST_ACCOUNT_URL,param -> {
            param.put("open_id", userId);
            param.put("account_type", 2);
        },String.class);
    }

    @Override
    public String registCustomerAccount(Long userId) {
        return this.invork(FadadaUrl.REGIST_ACCOUNT_URL,param -> {
            param.put("open_id", userId);

            param.put("account_type", 1);
        },String.class);
    }




    @Override
    public VervifyUrl getCustomerVervifyUrl(String customerId){
        VervifyUrl vervifyUrl = this.invork(FadadaUrl.PERSON_VERIFY_URL, params -> {
            params.put("customer_id", customerId);
            params.put("verified_way", getVerifyWay());
            params.put("page_modify", "2");
            params.put("notify_url", getVerifyNodifyUrl());
            params.put("customer_name", EMPTY_STR);
            params.put("customer_ident_type", "0");
            params.put("customer_ident_no", EMPTY_STR);
            params.put("mobile", EMPTY_STR);
            params.put("ident_front_path", EMPTY_STR);
            params.put("return_url", getVefiryReturnUrl());
            params.put("result_type", EMPTY_STR);
            params.put("cert_flag", EMPTY_STR);
        }, VervifyUrl.class);

        String url = vervifyUrl.getUrl();
        String bizContent = URLDecoder.decode(url, Charsets.UTF_8);
        url = new String(Base64.decode(bizContent.getBytes()));
        String replace = url.replace("https://realnameverify-test.fadada.com/", "https://realnameverify-test18.fadada.com/");
        String re = replace.replace("https://realnameverify.fadada.com/","https://realnameverify15.fadada.com");
        vervifyUrl.setUrl(re);
        return vervifyUrl;
    }

    private AccountVervify getAccountInfo(String transactionNo, Integer accountType){
        String url = accountType == 1 ? FadadaUrl.PERSON_VERIFY_INFO : FadadaUrl.COMPANY_VERIFY_INFO;
        return this.invork(url,param -> {
            param.put("verified_serialno", transactionNo);
        }, AccountVervify.class);
    }

    @Override
    public AccountVervify getCustomerVervifyInfo(String transNo) {
//        String url = accountType == 1 ? FadadaUrl.PERSON_VERIFY_INFO : FadadaUrl.COMPANY_VERIFY_INFO;
        return this.invork(FadadaUrl.PERSON_VERIFY_INFO,param -> {
            param.put("verified_serialno", transNo);
        }, AccountVervify.class);
    }

    @Override
    public CompanyVervify getCompanyVervifyInfo(String transNo) {
        return this.invork(FadadaUrl.COMPANY_VERIFY_INFO,param -> {
            param.put("verified_serialno", transNo);
        }, CompanyVervify.class);
    }


    @Override
    public String getSignUrl(String transId, String customerId, String contractCode,Integer type, String title ) {
        String keyword = type == 1 ? "甲方签约人" : "乙方签约人";

        return this.buildUrl(FadadaUrl.SIGN_URL,x -> {
            x.put("customer_id",customerId);
        },params -> {
            params.put("transaction_id",transId);
            params.put("contract_id",contractCode);
            params.put("doc_title",title);
            params.put("keyword_strategy", 2);
            params.put("sign_keyword",keyword);
            params.put("return_url", URLEncoder.DEFAULT.encode(getSignReturnUrl(), Charsets.UTF_8));
            params.put("notify_url",URLEncoder.DEFAULT.encode(getSignNodifyUrl(), Charsets.UTF_8));
        } ,transId);
    }



    @Override
    public Contract getContract(String templateId, String contractCode, String params) {
        Contract invork = this.invork(FadadaUrl.GENERATE_CONTRACT_URL, param -> {
            param.put("a", templateId);
            param.put("b", contractCode);
        }, Contract.class, p -> {
            p.put("template_id", templateId);
            p.put("contract_id", contractCode);
            p.put("parameter_map", params);
        }, params);

        String replace = invork.getDownloadUrl().replace("https://testapi.fadada.com:8443/", "https://testapi21.fadada.com/");
        String replace1 = invork.getViewpdfUrl().replace("https://testapi.fadada.com:8443/", "https://testapi21.fadada.com/");
        invork.setDownloadUrl(replace);
        invork.setViewpdfUrl(replace1);
        //此处不是用参数升序做加密  用ab强制排序。
        return invork;
    }


    @Override
    public void applyCert(String customerId, String transNo) {
        this.invork(FadadaUrl.APPLY_CERT, param -> {
            param.put("customer_id",customerId);
            param.put("verified_serialno",transNo);
        },Void.class);
    }

    @Override
    public void uploadTemplate(String templateCode, String docUrl) {
        docUrl = docUrl.replaceAll(" ", "%20");
        String docUrls = URLEncoder.DEFAULT.encode(docUrl,Charsets.UTF_8);
        this.invork(FadadaUrl.UPLOAD_TEMPLATE, param ->  param.put("template_id",templateCode),Void.class,x -> x.put("doc_url",docUrls),null);
    }

    @Override
    public String getContactViewUrl(String contractCode) {
        return this.buildUrl(FadadaUrl.VIEW_CONTRACT,x -> x.put("contract_id", contractCode),x -> {},"");
    }

    @Override
    public String downloadContract(String contractCode) {
        return this.buildUrl(FadadaUrl.DOWNLOAD_CONTRACT,x -> x.put("contract_id", contractCode),x -> {},"");
    }

    @Override
    public void fillingContract(String contractCode) {
        this.invork(FadadaUrl.FILLING_CONTRACT, param ->  param.put("contract_id",contractCode),Void.class);
    }


}
